﻿using System;
using System.Collections.Generic;
using System.Text;
 

namespace QQ.Util
{
    static class ConfigXML
    {
        static private System.Xml.XmlDocument xml;
        static private readonly string xmlFile = Environment.CurrentDirectory + "\\" + "config.xml";
        static private string RootNodeName = "Config"; 

        public static string VCode
        {
            get
            {
                return GetValue("VCode");
            }
            set
            {
                SetValue("VCode", value, true);
            }
        }


        public static string UserName
        {
            get
            {
                return GetValue("UserName");
            }
            set
            {
                SetValue("UserName", value, true);
            }
        }


        public static string PassWord
        {
            get
            {
                return GetValue("PassWord");
            }
            set
            {
                SetValue("PassWord", value, true);
            }
        }


        public static string SendUrl
        {
            get
            {
                return GetValue("SendUrl");
            }
            set
            {
                SetValue("SendUrl", value, true);
            }
        } 
         
        
         
        public static List<KeyValuePair<string, object>> GetValue(string name, KeyValuePair<string, object> keyValuePair)
        {
            List<KeyValuePair<string, object>> list = new List<KeyValuePair<string, object>>();
            if (xml == null)
            {
                Load();
            }
            System.Xml.XmlNode node = xml.DocumentElement.SelectSingleNode(name);
            if (node != null)
            {
                foreach (System.Xml.XmlNode childNode in node.ChildNodes)
                {
                    if (childNode != null && childNode.InnerText != "")
                    {
                        try
                        {
                            list.Add(new KeyValuePair<string, object>(childNode.Name, childNode.InnerText));
                        }
                        catch { }
                    }
                }
            }
            return list;
        }

        public static bool SetValue(string name, List<KeyValuePair<string, object>> value)
        {
            if (xml == null)
            {
                Load();
            }
            try
            {
                System.Xml.XmlNode node = xml.DocumentElement.SelectSingleNode(name);
                if (node != null)
                {
                    node.RemoveAll();
                }
                else
                {
                    System.Xml.XmlNode root = xml.SelectSingleNode(RootNodeName);
                    node = xml.CreateElement(name);
                    root.AppendChild(node);
                }
                foreach (KeyValuePair<string, object> pari in value)
                {
                    System.Xml.XmlElement xe = xml.CreateElement(pari.Key);
                    xe.InnerText = pari.Value != null ? pari.Value.ToString() : "";
                    node.AppendChild(xe);
                }
                return true;
            }
            catch { return false; }
        }


        public static bool Save()
        {
            if (xml == null)
            {
                return false;
            }
            try
            {
                xml.Save(xmlFile); return true;
            }
            catch { }
            return false;
        }

        public static string[] Content
        {
            get
            {
                List<KeyValuePair<string, object>> list =  GetValue("Content", new KeyValuePair<string, object>());
                string[] array = new string[list.Count];
                for (int i = 0; i < list.Count; i++)
                {
                    array[i] = list[i].Value != null ? list[i].Value.ToString() : "";
                }
                return array;
            }
            set
            {
                List<KeyValuePair<string, object>> list = new List<KeyValuePair<string, object>>();
                foreach (string str in value)
                {
                    list.Add(new KeyValuePair<string, object>("Item", str));
                }
                SetValue("Content", list);
                xml.Save(xmlFile);
            }
        }

        

        
        private static bool Load()
        {
            try
            {
                if (xml == null)
                {
                    if (!System.IO.File.Exists(xmlFile))
                    {
                        System.IO.FileStream stream = System.IO.File.Create(xmlFile);
                        stream.Close();
                    }
                    xml = new System.Xml.XmlDocument();
                    xml.Load(xmlFile);
                }
                return true;
            }
            catch (System.Xml.XmlException)
            {
                 try
                {
                    string str = global::RTGather.Properties.Resources.config;
                    xml.LoadXml(str);
                    xml.Save(xmlFile);
                    return true;
                }
                catch { return false; } 
            }
            catch
            {
                return false;
            }
        }

        public static bool IsLoad()
        {
            return Load();
        }

        public static string GetValue(string name)
        {
            try
            {
                if (xml == null)
                {
                    Load();
                }
                string type = xml.DocumentElement.SelectSingleNode(name).InnerText;
                return type;
            }
            catch (Exception)
            {
                return null;
            }
        }

        public static bool SetValue(string name, string value, bool aotoSave)
        {
            if (xml == null)
            {
                Load();
            }
            System.Xml.XmlNode node = xml.DocumentElement.SelectSingleNode(name);
            try
            {
                if (node == null)
                {
                    System.Xml.XmlNode root = xml.SelectSingleNode(RootNodeName);
                    System.Xml.XmlElement xe = xml.CreateElement(name);
                    xe.InnerText = value;
                    root.AppendChild(xe);
                }
                else
                {
                    node.InnerText = value;
                }
                if (aotoSave)
                {
                    xml.Save(xmlFile);
                }
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static bool SetValue(string name, string value)
        {
            return SetValue(name, value, true);
        }


    }
}

 